<?php

//小站选项应用模型Hot模块
class Hot_model extends Model {

	function Hot_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("accounts_model", "am");
		$this->load->model("plugins_model", "pm");
		$this->load->model("comment_model");
		$this->load->model("options_model");
		$this->userid = $this->am->is_logined();
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_hot")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_hot");
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_hot", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$r = $this->pm->get($row["plid"]);
			$row["moduleid"] = $r["moduleid"];
			//评论总数
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if($r) {
				$siteid = $r["siteid"];
				$row["siteid"] = $siteid;
				$commentcount = $this->comment_model->count_lists($siteid, $row["plid"], $row["id"]);
				$row["commentcount"] = $commentcount;
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
			
		}
		return $row;
	}
	
	//#查看内容
	function get_info($id)
	{
		$row = array();
		$query = $this->db->get_where("module_hot", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$r = $this->pm->get($row["plid"]);
			$row["moduleid"] = $r["moduleid"];
		}
		return $row;
	}
	
	//#获取列表
	function lists($plid)
	{
		$lists = array();
		$posids = array(1, 2, 3);//3个位置
		for($i=0; $i<count($posids); $i++) {
			$lists[] = $this->get_posid($plid, $posids[$i]);
		}
		return $lists;
	}
	
	//获取位置热点
	function get_posid($plid, $posid = 1)
	{
		$row = array();
		$query = $this->db->from("module_hot")->where(array("plid"=>$plid, "posid"=>$posid))->order_by("addtime", "desc")->get();
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//模块ID
			$plid = $row["plid"];
			$q = $this->db->get_where("site_plugins", array("id"=>$plid));
			$r = $q->row_array();
			$row["moduleid"] = $r["moduleid"];
		}
		return $row;
	}
	
	//添加
	//返回ID!
	function add($plid)
	{
		$title = strip_tags(p("title"));
		$posid = p("posid");
		$description = p("description");
		
		//验证图片
		$pics = get_content_pics($description);
		if(count($pics) == 0 || count($pics) > 5) {
			return false;
		}
		
		//第一张图片
		$firstpic = $pics[0];
		$filename = str_replace("/data/upload_ck/", "", $firstpic);
		$ext = get_imgext($firstpic);
		$newfilename = str_replace(".".$ext, "", $filename)."_crop.".$ext;
		//裁剪图片
		$params = array('src_file' => './data/upload_ck/'.$filename, 'dst_file' => './data/upload_ck/'.$newfilename);
		//$this->load->library("imagecrop", $params);
		$this->load->library("imagecrop");
		$this->imagecrop->src_file=$params['src_file'];
		$this->imagecrop->dst_file=$params['dst_file'];
		$this->imagecrop->LoadImage();
		$this->imagecrop->Crop(300,300,1);
		$this->imagecrop->SaveImage();
		$this->imagecrop->destory();
		$firstpic = "/data/upload_ck/".$newfilename;
		
		$data = array(
			"firstpic" => $firstpic,
			"userid" => $this->userid,
			"title" => $title,
			"description" => $description,
			"posid" => $posid,
			"plid" => $plid,
			"addtime" => time(),
		);
		$result = $this->db->insert("module_hot", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//添加
	//返回ID!
	function edit($plid)
	{
		$id = p("id");
		$title = strip_tags(p("title"));
		$posid = p("posid");
		$description = p("description");
		
		//验证图片
		$pics = get_content_pics($description);
		if(count($pics) == 0 || count($pics) > 5) {
			return false;
		}
		
		//第一张图片
		$firstpic = $pics[0];
		$filename = str_replace("/data/upload_ck/", "", $firstpic);
		$ext = get_imgext($firstpic);
		$newfilename = str_replace(".".$ext, "", $filename)."_crop.".$ext;
		//裁剪图片
		$params = array('src_file' => './data/upload_ck/'.$filename, 'dst_file' => './data/upload_ck/'.$newfilename);
		//$this->load->library("imagecrop", $params);
		$this->load->library("imagecrop");
		$this->imagecrop->src_file=$params['src_file'];
		$this->imagecrop->dst_file=$params['dst_file'];
		$this->imagecrop->LoadImage();
		$this->imagecrop->Crop(300,300,1);
		$this->imagecrop->SaveImage();
		$this->imagecrop->destory();
		$firstpic = "/data/upload_ck/".$newfilename;
	
		$data = array(
			"firstpic" => $firstpic,
			"userid" => $this->userid,
			"title" => $title,
			"description" => $description,
			"posid" => $posid,
			"plid" => $plid,
			"addtime" => time(),
		);
		$this->db->where("id", $id);
		$result = $this->db->update("module_hot", $data);
		return $result;
	}
	
	//删除
	function delete($dataid)
	{
		$result = $this->db->delete("module_hot", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择话题
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_hot");
		return $result;
	}
	
	//预览
	function preivew()
	{
		//a(p("title"));
		$viewdata = array();
		$id = 0;
		if(p("title") && p("posid")) {
			$viewdata["title"] = strip_tags(p("title"));
			$viewdata["posid"] = p("posid");
			$viewdata["description"] = p("description");
			$viewdata["id"] = p("id");
		}
		return $viewdata;
	}
	
	
}

/* End of file */